package com.carcenter.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carcenter.common.constant.redis.RedisConstant;
import com.carcenter.store.entity.AddressVo;
import com.carcenter.store.entity.DefaultAddress;
import com.carcenter.store.entity.StoreAddress;
import com.carcenter.store.entity.StoreChina;
import com.carcenter.store.service.IStoreChinaService;
import com.carcenter.store.service.StoreAddressService;
import com.carcenter.store.mapper.StoreAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @author 龙龙与贝儿
* @description 针对表【store_address】的数据库操作Service实现
* @createDate 2025-07-20 11:17:30
*/
@Service
public class StoreAddressServiceImpl extends ServiceImpl<StoreAddressMapper, StoreAddress>
    implements StoreAddressService{

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private StoreAddressMapper storeAddressMapper;

    @Autowired
    IStoreChinaService storeChinaService;

    @Override
    public List<AddressVo> updateAddressCashe(Integer storeId) {
        QueryWrapper<StoreAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<StoreAddress> storeAddressList = storeAddressMapper.selectList(queryWrapper);

        List<AddressVo> addressVos = new ArrayList<>();
        for (StoreAddress storeAddress : storeAddressList) {
            AddressVo addressVo = new AddressVo();
            addressVo.setId(storeAddress.getId());
            addressVo.setIsDefault(storeAddress.getIsDefault());
            addressVo.setAddressData(storeAddress.getProvince()+"\t"+storeAddress.getCity()+"\t"+storeAddress.getArea()+"\t"+storeAddress.getStreet());
            addressVos.add(addressVo);
        }

        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(RedisConstant.STORE_ADDRESS+":"+storeId, addressVos);
        return addressVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreAddress newAddress(StoreAddress storeAddress) {
        if (storeAddress.getIsDefault() == 1) {
            storeAddressMapper.update(null,
                    Wrappers.<StoreAddress>lambdaUpdate(StoreAddress.class)
                    .eq(StoreAddress::getStoreId, storeAddress.getStoreId())
                    .set(StoreAddress::getIsDefault, 0)
                    );
        }
        storeAddress.setProvince(storeChinaService.getOne(Wrappers.lambdaQuery(StoreChina.class)
                .eq(StoreChina::getId, storeAddress.getProvince())).getName());
        storeAddress.setCity(storeChinaService.getOne(Wrappers.lambdaQuery(StoreChina.class)
                .eq(StoreChina::getId, storeAddress.getCity())).getName());
        storeAddress.setArea(storeChinaService.getOne(Wrappers.lambdaQuery(StoreChina.class)
                .eq(StoreChina::getId, storeAddress.getArea())).getName());
        storeAddressMapper.insert(storeAddress);
        this.updateAddressCashe(storeAddress.getStoreId());
        StoreAddress storeAddress1 = storeAddressMapper.selectById(storeAddress.getId());

        return storeAddress1;
    }

    @Override
    public List<DefaultAddress> getDefaultAddressListByStoreIds(List<Integer> storeIds) {
        LambdaQueryWrapper<StoreAddress> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<StoreAddress> wrapper = queryWrapper.in(StoreAddress::getStoreId, storeIds)
                .eq(StoreAddress::getIsDefault, 1);
        List<StoreAddress> storeAddressList = storeAddressMapper.selectList(queryWrapper);
        List<DefaultAddress> defaultAddressList = new ArrayList<>();
        for (StoreAddress storeAddress : storeAddressList) {
            DefaultAddress defaultAddress = new DefaultAddress();
            defaultAddress.setStoreId(storeAddress.getStoreId());
            defaultAddress.setDefaultAddressId(storeAddress.getId());
            defaultAddress.setDefaultAddressData(storeAddress.getProvince()+"\t"+storeAddress.getCity()+"\t"+storeAddress.getArea()+"\t"+storeAddress.getStreet());
            defaultAddressList.add(defaultAddress);
        }
        return defaultAddressList;

    }
}




